﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCCN.DAL;
using SCCN.BLL;
using SCCN.Utility;

public partial class Admin_AdvertisementList : System.Web.UI.Page
{
    AdvertisementBLL bllAdvertisement = AdvertisementBLL.getInstance();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRpt();
        }
    }

    void BindRpt()
    {
        int adSpaceId = 0;
        int.TryParse(HttpHelper.GetParameters("adspaceid", HttpHelper.MethodType.GET), out adSpaceId);
        SC_Advertisement obj = new SC_Advertisement();
        if (this.txtAdDesc.Text.Length > 0)
        {
            obj.AdDesc = this.txtAdDesc.Text;
        }
        else
        {
            obj.AdDesc = string.Empty;
        }
        if (this.txtAdvertisementName.Text.Length > 0)
        {
            obj.AdName = this.txtAdvertisementName.Text;
        }
        else
        {
            obj.AdName = string.Empty;
        }
        obj.AdSpaceId = adSpaceId;
        if (this.lblPicSrc.Text.Length > 0)
        {
            obj.PicSrc = this.lblPicSrc.Text;
        }
        else
        {
            obj.PicSrc = string.Empty;
        }
        obj.IsDelete = false;
        //obj.IsPic = this.drpIsPic.SelectedValue == "1" ? true : false;
        int rowCount = 0;
        this.rptList.DataSource = bllAdvertisement.GetAdvertisementByCondition(obj, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out rowCount);
        this.rptList.DataBind();
        this.AspNetPager1.RecordCount = rowCount;
    }

    /// <summary>
    /// Reapeater命令事件
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = 0;
        int.TryParse(e.CommandArgument.ToString(), out id);
        if (e.CommandName == "Del")
        {
            if (bllAdvertisement.Delete(id))
            {
                HttpHelper.Alert("删除成功！", Page);
                BindRpt();
            }
            else
            {
                HttpHelper.Alert("删除失败！", Page);
            }
        }
        if (e.CommandName == "Update")
        {
            SC_Advertisement obj = bllAdvertisement.GetAdvertisementById(id);
            this.txtAdDesc.Text = obj.AdDesc;
            this.txtAdvertisementName.Text = obj.AdName;
            this.drpIsPic.SelectedValue = obj.IsPic.ToString() == "True" ? "1" : "2";
            this.txtOrderId.Text = obj.OrderId.ToString();
            this.lblPicSrc.Text = obj.PicSrc;
            this.txtTargetUrl.Text = obj.TargetUrl;
            this.hid.Value = id.ToString();
        }
    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.fup.HasFile)
        {
            string FileName = fup.PostedFile.FileName;
            string upload_file = Server.MapPath("~/") + "upload/advertisement/" + FileName;
            fup.PostedFile.SaveAs(upload_file);//开始上传文件
            this.lblPicSrc.Text = "/upload/advertisement/" + FileName; ;
        }
        if (this.hid.Value.Length == 0)
        {
            try
            {
                
                SC_Advertisement obj = new SC_Advertisement();
                obj.AdName = this.txtAdvertisementName.Text;
                obj.AdDesc = this.txtAdDesc.Text;
                obj.AdSpaceId = Convert.ToInt32(HttpHelper.GetParameters("adspaceid", HttpHelper.MethodType.GET));
                obj.IsDelete = false;
                obj.IsPic = this.drpIsPic.SelectedValue == "1" ? true : false;
                obj.PicSrc = this.lblPicSrc.Text;
                obj.OrderId = Convert.ToInt32(this.txtOrderId.Text);
                obj.TargetUrl = this.txtTargetUrl.Text;
                if (bllAdvertisement.Add(obj))
                {
                    HttpHelper.Alert("保存成功！", Page);

                    this.txtAdDesc.Text = string.Empty;
                    this.txtAdvertisementName.Text = string.Empty;
                    this.txtOrderId.Text = string.Empty;
                    this.txtTargetUrl.Text = string.Empty;
                    this.lblPicSrc.Text = string.Empty;
                    this.hid.Value = "0";
                    BindRpt();
                }
                else
                {
                    HttpHelper.Alert("保存失败！", Page);
                }
            }
            catch (Exception ex)
            {
                HttpHelper.Alert(ex.Message.ToString(), Page);
            }
        }
        else
        {
            int id = 0;
            int.TryParse(this.hid.Value, out id);
            SC_Advertisement obj = bllAdvertisement.GetAdvertisementById(id);
            obj.AdDesc = this.txtAdDesc.Text;
            obj.AdName = this.txtAdvertisementName.Text;
            obj.AdSpaceId = Convert.ToInt32(HttpHelper.GetParameters("adspaceid", HttpHelper.MethodType.GET));
            obj.IsDelete = false;
            obj.IsPic = this.drpIsPic.SelectedValue == "1" ? true : false;
            obj.PicSrc = this.lblPicSrc.Text;
            obj.OrderId = Convert.ToInt32(this.txtOrderId.Text);
            obj.TargetUrl = this.txtTargetUrl.Text;
            if (bllAdvertisement.Update(obj))
            {
                HttpHelper.Alert("保存成功！", Page);

                this.txtAdDesc.Text = string.Empty;
                this.txtAdvertisementName.Text = string.Empty;
                this.txtOrderId.Text = string.Empty;
                this.txtTargetUrl.Text = string.Empty;
                this.lblPicSrc.Text = string.Empty;
                this.hid.Value = "0";
                BindRpt();
            }
            else
            {
                HttpHelper.Alert("保存失败！", Page);
            }
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        this.drpIsPic.SelectedIndex = -1;
        this.txtAdvertisementName.Text = string.Empty;
        this.txtAdDesc.Text = string.Empty;
        this.lblPicSrc.Text = string.Empty;
        this.hid.Value = string.Empty;
    }
}